package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.annotation.MyLog;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import com.qf.utils.R;
import com.qf.vo.StudentVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zdl
 * @date 2025/2/17
 */
@CrossOrigin
@RestController
@RequestMapping("/stu")
public class StudentController {
    @Resource
    private StudentService studentService;

    /**
     * 根据id 查询全部学生
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @MyLog("根据id查询学生")
    public R<Student> findStudentById(@PathVariable("id") int id) {
        return R.ok(studentService.findStudentById(id));
    }

    /**
     * 分页查询全部学生
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/all")
    @MyLog("分页查询全部学生")
    public R<PageInfo<Student>> findAllStudens(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> allStudents = studentService.findAllStudents();
        PageInfo<Student> studentPageInfo = new PageInfo<>(allStudents);
        return R.ok(studentPageInfo);
    }

    /**
     * 根据sname age 进行模糊 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param sname
     * @param age
     * @return
     */
    @GetMapping("/like")
    @MyLog("模糊查询")
    public R<PageInfo<Student>> findStudensLikeSnameAndAge(@RequestParam int pageNum, @RequestParam int pageSize, String sname, Integer age) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> allStudents = studentService.findStudentLikeSnameAndAge(sname, age);
        PageInfo<Student> studentPageInfo = new PageInfo<>(allStudents);
        return R.ok(studentPageInfo);
    }

    /**
     * 新增
     *
     * @param student
     * @return
     */
    @PostMapping("/add")
    @MyLog("新增学生")
    public R<Integer> addStudent(@RequestBody Student student) {
        System.out.println("添加用户信息:"+student);
        return R.ok(studentService.addStudent(student));
    }

    /**
     * 修改
     *
     * @param student
     * @return
     */
    @PostMapping("/update")
    @MyLog("修改学生")
    public R<Integer> updateStudent(@RequestBody Student student) {
        return R.ok(studentService.updateStudent(student));
    }

    /**
     * 根据 id  进行 单条 或 单条数据删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delBatch")
    public R<Integer> delBatchStudentById(@RequestBody List<Integer> id) {
        return R.ok(studentService.delBatchStudentByIds(id));
    }

    /**
     * 根据 id  单条 删除
     *
     * @param id
     * @return
     */
    @GetMapping("del/{id}")
    public R<Integer> delStudentById(@PathVariable("id") Integer id) {
        System.out.println(id);
        return R.ok(studentService.delStudentById(id));
    }

    /**
     * 查询所有学生信息以及关联的班级信息
     *
     * @return
     */
    @GetMapping("/class")
    public R<List<StudentVo>> findStudentAndClassRoom() {
        return R.ok(studentService.findAllStudentAndClassRoom());
    }

    /**
     * 根据id查询1个学生信息以及关联的所有学科信息以及每个学科关联的老师信息
     *
     * @param sid
     * @return
     */
    @GetMapping("/subjects")
    public R<StudentVo> findStudentAndAllSubjectAndTeacherByStuid(Integer sid) {
        return R.ok(studentService.findStudentAndAllSubjectAndTeacherBySid(sid));
    }
}
